<template>
	<view class="login-bg">
		<view class="login-container">
			<image class="logo" src="https://cdn.uviewui.com/uview/common/logo.png"></image>
			<view class="title">欢迎登录</view>
			<u-form :model="form" class="login-form">
				<u-form-item prop="mobile">
					<u-input v-model="form.mobile" placeholder="请输入手机号" clearable prefixIcon="phone" type="number"
						border="surround" shape="circle" />
				</u-form-item>
				<u-form-item prop="password">
					<u-input v-model="form.password" :type="showPwd ? 'text' : 'password'" placeholder="请输入密码" clearable
						prefixIcon="lock" border="surround" shape="circle" :suffixIcon="showPwd ? 'eye' : 'eye-off'"
						@suffix-icon-click="togglePwd" />
				</u-form-item>

				<u-button type="primary" shape="circle" @click="onLogin" :loading="loading" class="login-btn">
					登录
				</u-button>

				<view class="action-row">
					<view><up-text type="error" @click="onForget" text="忘记密码?"></up-text></view>
					<view><up-text type="primary" @click="onRegister" text="注册账号"></up-text></view>
				</view>
			</u-form>
		</view>
		<up-toast ref="uToastRef"></up-toast>
	</view>
</template>

<script setup lang="ts">
	import { ref } from 'vue'
	import { loginApi } from '../../http/user';
	import { onShow } from '@dcloudio/uni-app'

	interface LoginForm {
		mobile : string
		password : string
	}

	const form = ref<LoginForm>({
		mobile: '',
		password: ''
	})

	const uToastRef = ref(null)
	function showToast(params : any) {
		uToastRef.value.show({
			...params,
			complete() {

			}
		});
	}

	const showPwd = ref(false)
	const loading = ref(false)

	// 判断是否已登录
	const checkLogin = () => {
		const token = uni.getStorageSync('accessToken')
		if (token) {
			uni.reLaunch({
				url: '/pages/index/index'
			})
		}
	}

	onShow(() => {
		checkLogin()
	})

	const onLogin = () => {
		if (!form.value.mobile) {
			showToast({
				"message": '请输入手机号'
			})
			return
		}
		if (!form.value.password) {
			showToast({
				"message": '请输入密码'
			})
			return
		}
		loginApi(form.value.mobile, form.value.password).then(data => {
			uni.setStorageSync('accessToken', data.accessToken)
			uni.setStorageSync('tokenType', data.tokenType)
      uni.setStorageSync('studentId', data.id) // 假设后端返回了 studentId
      uni.reLaunch({
				url: '/pages/login/login'
			})
		})
	}

	const onForget = () => {
		showToast({
			"message": '请联系管理员找回密码'
		})
	}

	const onRegister = () => {
		showToast({
			"message": '请联系管理员申请'
		})
	}

	const togglePwd = () => {
		showPwd.value = !showPwd.value
	}
</script>

<style lang="scss" scoped>
	.login-bg {
		min-height: 100vh;
		width: 100vw;
		background: linear-gradient(135deg, #e0eafc 0%, #cfdef3 100%);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.login-container {
		background: #fff;
		border-radius: 24rpx;
		box-shadow: 0 8rpx 32rpx rgba(180, 200, 220, 0.2);
		padding: 60rpx 40rpx 48rpx 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		min-width: 600rpx;
	}

	.logo {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		margin-bottom: 30rpx;
		box-shadow: 0 4px 24px #b3c0d1;
	}

	.title {
		font-size: 38rpx;
		font-weight: bold;
		margin-bottom: 50rpx;
		color: #333;
		letter-spacing: 2rpx;
	}

	.login-form {
		width: 100%;
	}

	.u-form-item {
		margin-bottom: 24rpx;
	}

	.action-row {
		display: flex;
		justify-content: space-between;
		margin: 20rpx 0 40rpx;
		width: 100%;
		padding-top: 20px;
	}

	.login-btn {
		width: 100%;
		margin-top: 20rpx;
		font-size: 32rpx;
	}
</style>